Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use form for expression validation in alert profiles #2667

Merged
merged 3 commits into from
Sep 6, 2023

Conversation

hmpf
Copy link
Contributor

@hmpf hmpf commented Aug 29, 2023

Closes #1876.

Improvement of #2649.

This moves the validation for expressions to django forms and also fixes the existing Expression form to correctly handle both string inputs, in case of IP addresses e.g. and multiple choice inputs.

@codecov
Copy link

codecov bot commented Aug 29, 2023

Codecov Report

Merging #2667 (8929cf3) into 5.6.x (92acff9) will increase coverage by 0.18%.
The diff coverage is 93.75%.

❗ Current head 8929cf3 differs from pull request most recent head ef27237. Consider uploading reports for the commit ef27237 to get more accurate results

@@            Coverage Diff             @@
##            5.6.x    #2667      +/-   ##
==========================================
+ Coverage   54.13%   54.31%   +0.18%     
==========================================
  Files         558      558              
  Lines       40641    40671      +30     
==========================================
+ Hits        22000    22090      +90     
+ Misses      18641    18581      -60     
Files Changed Coverage Δ
python/nav/web/alertprofiles/views.py 42.92% <85.71%> (+2.76%) ⬆️
python/nav/web/alertprofiles/forms.py 69.25% <97.05%> (+13.47%) ⬆️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link

github-actions bot commented Aug 29, 2023

Test results

     12 files       12 suites   13m 51s ⏱️
3 242 tests 3 146 ✔️   96 💤 0
9 201 runs  8 913 ✔️ 288 💤 0

Results for commit ef27237.

♻️ This comment has been updated with latest results.

@johannaengland johannaengland changed the base branch from master to 5.6.x August 30, 2023 07:20
@johannaengland johannaengland changed the title Working ip validation Use form for expression validation in alert profiles Aug 30, 2023
Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad you're doing this cleanup :)

This isn't a full review (I'm sure @hmpf knows the form stuff better than me), but I'm not sure we've thought of this part: What about existing broken data?

The simple solution would be to just let AlertEngine log its brains out when existing data isn't valid (and pray that someone actually looks at the logs). Or, is there someway more in-your-face way we can inform the user that their profile is broken?

python/nav/web/alertprofiles/forms.py Outdated Show resolved Hide resolved
python/nav/web/alertprofiles/views.py Show resolved Hide resolved
Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested manually this time, and I notice two issues:

  1. If I select "in match" from the dropdown and enter an invalid value, my dropdown selection is reset to "equals match" when the form displays the validation error. Not sure if this problem was there before (I guess there was no validation, so nobody ever noticed). This should be fixed, IMHO.

  2. Also, is my value supposed to be blanked out from the form if it was invalid? What if I input a really long string and only had a small typo? Now I have to enter everything all over again, rather than just fix the one typo...

  3. I noticed the usage examples quoted on the form page includes CIDR addresses, not just plain IP address. I'm not entirely sure these are supposed to work (but it is more likely than not), you should check how AlertEngine actually parses the values. If they are meant to work, someone has definitely used CIDR values in their profile, and then these must also validate. For this, we have nav.util.is_valid_cidr(). You would have to use something like is_valid_ip(ip) or is_valid_cidr(ip) for the form logic...

@johannaengland
Copy link
Contributor

  1. I added validation for CIDR addresses, I have tried to figure out if they are supposed to work and I think they do, but I am not 100% certain since the alert engine code is quite complicated.

For 1. and 2. I added a new issue (#2673) since this requires a lot more effort and probably a bigger restructuring of the alert profile views.

@lunkwill42
Copy link
Member

  1. I added validation for CIDR addresses, I have tried to figure out if they are supposed to work and I think they do, but I am not 100% certain since the alert engine code is quite complicated.

Great!

For 1. and 2. I added a new issue (#2673) since this requires a lot more effort and probably a bigger restructuring of the alert profile views.

Agreed - as discussed IRL, these issues are likely not related to this PR at all, just to the fact that Alert Profiles predates Django and there has a very wonky usage of Django functionality.

Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Don't forget to squash your fixup!s before merging! :)

@sonarcloud
Copy link

sonarcloud bot commented Sep 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Alert Profiles does not validate IP addresses in filter expressions
3 participants